package cn.xny.org.service.impl;

import cn.xny.basic.exception.BusinessException;
import cn.xny.basic.util.BaiduAuditUtils;
import cn.xny.basic.util.Md5Utils;
import cn.xny.basic.util.StrUtils;
import cn.xny.org.domain.Employee;
import cn.xny.org.domain.Shop;

import cn.xny.basic.service.impl.BaseServiceImpl;
import cn.xny.org.domain.ShopAuditLog;
import cn.xny.org.dto.ShopDto;
import cn.xny.org.mapper.EmployeeMapper;
import cn.xny.org.mapper.ShopAuditLogMapper;
import cn.xny.org.mapper.ShopMapper;
import cn.xny.org.service.ShopService;
import cn.xny.user.domain.Logininfo;
import cn.xny.user.mapper.LogininfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements ShopService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
     private LogininfoMapper logininfoMapper;

    @Override
    public void settlement(Shop shop) {
        //一：校验
        //1.空值校验
        if(StringUtils.isEmpty(shop.getTel()) ||StringUtils.isEmpty(shop.getName())){
            throw new BusinessException("信息不能为空");
        }
        //2.两次密码不一样校验
        if(! shop.getAdmin().getComfirmPassword().equals(shop.getAdmin().getPassword())){
            throw new BusinessException("两次密码不一样");

        }
        //3.当前店铺是否入驻
            Shop dbShop = shopMapper.findByNameAddAddress(shop);
        if(dbShop!=null){

            throw new BusinessException("当前店铺已入驻过，请直接登录");
        }

        //二：百度AI智能审核【店铺名，log是否合法】
        if(!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("店铺名字不合格！！！");
        }
        if (!BaiduAuditUtils.ImgCensor("http://123.207.27.208"+shop.getLogo())){

            throw new BusinessException("店铺Logo不合格！！！");
        }

        //三：添加到数据库
        //1.先添加员工信息
        Employee admin=shop.getAdmin();
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(admin.getPassword() + salt);
        admin.setSalt(salt);
        admin.setPassword(md5Pwd);

        //添加logininfo信息
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(admin,logininfo);
        logininfo.setType(0);//管理员
        logininfo.setDisable(true);//可以登陆
        logininfoMapper.save(logininfo);

        //三：数据库操作
        //1.保存t_employee员工信息 - 生成自增id
        admin.setLogininfo_id(logininfo.getId());
        employeeMapper.save(admin);//只要添加之后，就生成了自增id

        //2.将自增id设置到shop对象，并保存店铺信息 - 生成店铺的自增id
        shop.setAdminId(admin.getId());
        shopMapper.save(shop);

        //3.将店铺的shopid设置到employee对象，修改employee信息
        admin.setShop_id(shop.getId()); //admin对象：id，shopId
        employeeMapper.update(admin);

    }
    //四：人工审核过后发送激活邮件
    //审核通过
    @Override
    public void auditPass(ShopAuditLog log) throws MessagingException {
        shopMapper.updateState(2,log.getShopId());
        log.setState(1);
        log.setAuditId(350L);
        shopAuditLogMapper.save(log);

        //3.发邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        helper.setFrom("1924562332@qq.com");
        helper.setSubject("店铺激活邮件");
        Shop shop = shopMapper.loadByAll(log.getShopId());
        //true - 能够再内容中编写html标签 - 会解析
        helper.setText("<h1>你的店铺已经注册!!!</h1><img src='http://123.207.27.208/"+
                shop.getLogo()+"' ><a href='http://127.0.0.1:8080/shop/active/"+log.getShopId()+"'>点击该链接激活</a>",true);
        //店铺管理员

        Employee admin = employeeMapper.loadByAll(shop.getAdminId());
        helper.setTo(admin.getEmail());
        //发送
        javaMailSender.send(mimeMessage);
    }

    //审核失败
    @Override
    public void auditReject(ShopAuditLog log) throws MessagingException {
        shopMapper.updateState(4,log.getShopId());
        log.setState(0);
        log.setAuditId(350L);
        shopAuditLogMapper.save(log);

        //3.发邮件

        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("1924562332@qq.com");
        //邮件主题
        mailMessage.setSubject("店铺激活邮件");
        Shop shop = shopMapper.loadByAll(log.getShopId());
        //true - 能够再内容中编写html标签 - 会解析
        mailMessage.setText("<h3>你的店铺已经审核未通过</h3>");
        //店铺管理员

        Employee admin = employeeMapper.loadByAll(shop.getAdminId());
        mailMessage.setTo(admin.getEmail());
        //发送邮件
        javaMailSender.send(mailMessage);
    }


    //店铺状态激活
    @Override
    public void auditActive(Long shopId) {



        shopMapper.updateState(3,shopId);


    }

    @Override
    public List<ShopDto> getCountByState() {

        return  shopMapper.getCountByState();
    }
}
